/**
 *====================================================
 * 文件名称: StatisticsEnterpriseCastor.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年11月13日			chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.statistics.action.v1.dto;

import java.util.ArrayList;
import java.util.List;

import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.bulkstock.service.BulkstockInfoService;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.member.action.v1.model.FinanceBulkstockPledgeRateDetail;
import com.yuexunit.finance.member.entity.EnterpriseEntity;
import com.yuexunit.finance.member.entity.EnterpriseGradeEntity;
import com.yuexunit.finance.member.entity.FinanceBulkstockPledgeRateEntity;
import com.yuexunit.finance.member.service.EnterpriseGradeService;
import com.yuexunit.finance.member.service.EnterpriseService;
import com.yuexunit.finance.member.service.FinanceBulkstockPledgeRateService;
import com.yuexunit.finance.statistics.action.v1.model.EnterpriseFinanceSummaryDetail;
import com.yuexunit.finance.tatistics.entity.EnterpriseFinanceSummaryEntity;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: StatisticsEnterpriseCastor
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年11月13日 上午9:50:19
 */
public class StatisticsEnterpriseCastor {

	public static EnterpriseFinanceSummaryDetail toEnterpriseFinanceSummaryDetail(EnterpriseFinanceSummaryEntity entity) {
		EnterpriseGradeService enterpriseGradeService = ComponentFactory.loadClass(EnterpriseGradeService.class);
		FinanceBulkstockPledgeRateService financeBulkstockPledgeRateService = ComponentFactory.loadClass(FinanceBulkstockPledgeRateService.class);
		BulkstockInfoService bulkstockInfoService = ComponentFactory.loadClass(BulkstockInfoService.class);
		EnterpriseFinanceSummaryDetail detail = new EnterpriseFinanceSummaryDetail();
		EnterpriseGradeEntity grade = enterpriseGradeService.inquireEnterpriseGrade(entity.getEnterpriseId());
		EnterpriseService enterpriseService = ComponentFactory.loadClass(EnterpriseService.class);
		BeanUtils.copyProperties(entity, detail);
		EnterpriseEntity enterprise = enterpriseService.inquireEnterprise(entity.getEnterpriseId());
		if (StringUtil.isNotBlank(enterprise.getEnterpriseShortName())) {
			detail.setEnterpriseName(enterprise.getEnterpriseShortName());
		}
		if (grade!=null && grade.getGradeEvaluationId() != null) {
			detail.setEffectiveStart(DateUtils.fromDateInt(grade.getEffectiveStartDate()));
			detail.setEffectiveEnd(DateUtils.fromDateInt(grade.getEffectiveEndDate()));
			detail.setEffectiveDays(DateUtils.daysBetween(detail.getEffectiveStart(), detail.getEffectiveEnd()));
			List<FinanceBulkstockPledgeRateEntity> fbpres = financeBulkstockPledgeRateService.inquireFinanceBulkstockPledgeRateList(grade.getGradeEvaluationId());
			List<FinanceBulkstockPledgeRateDetail> fbprds = new ArrayList<>();
			for (FinanceBulkstockPledgeRateEntity fbpe : fbpres) {
				BulkstockInfoEntity bie = bulkstockInfoService.inquireBulkstockInfo(fbpe.getBulkstockInfoId());
				FinanceBulkstockPledgeRateDetail d = new FinanceBulkstockPledgeRateDetail();
				BeanUtils.copyProperties(fbpe, d);
				d.setBulkstockName(bie.getBulkstockName()+"("+bie.getSpecificationName()+")");
				fbprds.add(d);
			}
			detail.setFinanceBulkstockPledgeRateList(fbprds);
		}
		return detail;
	}

}
